package com.itheima.config;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

import javax.servlet.Filter;

// 把ServletInitConfig 配置文件加载到tomcat容器中      生成容器
// 配置拦截路径  --》》 找到指点的controller 映射路径
public class ServletInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    //  其他资源的配置   不参与映射
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    // 加载配置
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    // 配置拦截路径  --》》 找到指点的controller 映射路径
    protected String[] getServletMappings() {
        return new String[]{"/"};  // 拦截路径
    }


    @Override  //乱码处理     接收参数乱码 Post 乱码
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        return new Filter[]{filter};
    }


/*
    // 把ServletInitConfig 配置文件加载到tomcat容器中
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.registerShutdownHook();
        context.register(SpringMvcConfig.class);
        return context;
    }

    // 配置拦截路径  --》》 找到指点的controller 映射路径
    protected String[] getServletMappings() {
        return new String[]{"/"};    // 拦截路径 /表示拦截所有
    }


    //  其他资源的配置   不参与映射
    protected WebApplicationContext createRootApplicationContext() {
       *//* AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.registerShutdownHook();
        context.register(SpringConfig.class);
        return context;*//*

        return null;

    }*/
}
/*
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigClasses() {
        return new Class[0];
    }

    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    //乱码处理
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        return new Filter[]{filter};
    }
}*/
